﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class mobile_ajaxdata_ShopTarget : BaseDataPage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
         string aciton = Request.Params["aciton"];
         switch (aciton)
         {
             case "GetTargetList":
                 GetTargetList();
                 break;
             case "SaveTarget":
                 SaveTarget();
                 break;
             case "GetTargetPageList":
                 GetTargetPageList();
                 break;
             case "GetDayTargetList":
                 GetDayTargetList();
                 break;
             case "SaveDayTarget":
                 SaveDayTarget();
                 break;
             case "GetDayTargetPageList":
                 GetDayTargetPageList();
                 break;
             case "CheckDayTarget":
                 CheckDayTarget();
                 break;
             case "GetTargetSaleByShopRpt":
                 GetTargetSaleByShopRpt();
                 break;
         }
    }

    private void GetTargetSaleByShopRpt()
    {
        string shopid = Request.Params["shopid"];
        string finddate = Request.Params["finddate"];

        string sql = @"KM_GetTargetSaleByShopRpt '"+@finddate+"','"+@shopid+"'";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                    + "\",\"shopcode\":\"" + dr["shopcode"]
                    + "\",\"shopname\":\"" + dr["shopname"]
                    + "\",\"daytargetamt\":\"" + dr["daytargetamt"]
                  + "\",\"salewcl\":\"" + dr["salewcl"]
                  + "\",\"salecount\":\"" + dr["salecount"]
                  + "\",\"saleamt\":\"" + dr["saleamt"]
                  + "\",\"cashamt\":\"" + dr["cashamt"]
                  + "\",\"bankamt\":\"" + dr["bankamt"]
                  + "\",\"ldv\":\"" + dr["ldv"]
                  + "\",\"kdj\":\"" + dr["kdj"]
                  + "\",\"vipcount\":\"" + dr["vipcount"]
                  + "\",\"vipsaleamt\":\"" + dr["vipsaleamt"]
                  + "\",\"vipsalezb\":\"" + dr["vipsalezb"]
                  + "\",\"fzsaleamt\":\"" + dr["fzsaleamt"]
                  + "\",\"pjsaleamt\":\"" + dr["pjsaleamt"]
                  + "\",\"zsaleamt\":\"" + dr["zsaleamt"]
                  + "\",\"weektargetamt\":\"" + dr["weektargetamt"]
                  + "\",\"zwcl\":\"" + dr["zwcl"]
                  + "\",\"msaleamt\":\"" + dr["msaleamt"]
                  + "\",\"targetmoney\":\"" + dr["targetmoney"]
                  + "\",\"viptotcount\":\"" + dr["viptotcount"]
                  + "\",\"mwcl\":\"" + dr["mwcl"]
                  + "\",\"mvipzb\":\"" + dr["mvipzb"]
                  + "\",\"vipmsaleamt\":\"" + dr["vipmsaleamt"]
                  + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetDayTargetPageList()
    {
        string shopid = Request.Params["shopid"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);

        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by createdate desc) as rownumber,
                            shopid,convert(varchar(20),targetdate,23)as targetdate,targetamt from bas_shoptargetday
                        ) A
                        where rownumber > " + showcount.ToString() + " and shopid='" + shopid + "'";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                  + "\",\"targetdate\":\"" + dr["targetdate"]
                  + "\",\"targetamt\":\"" + dr["targetamt"]
                  + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
    private void CheckDayTarget()
    {
        string shopid = Request.Params["shopid"];
        string targetdate = Request.Params["targetdate"];

        string sql = "select * from bas_shoptargetday where shopid='"+shopid+"' and convert(varchar(20),targetdate,23) = '"+targetdate+"'";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Response.Write("SUCCESS");
            Response.End();
        }
        else
        {
            Response.Write("NODATA");
            Response.End();
        }

    }
    private void SaveDayTarget()
    {
        string shopid = Request.Params["shopid"];
        string shopname = Request.Params["shopname"];
        string targetdate = Request.Params["targetdate"];
        string clientcount = Request.Params["clientcount"];
        string targetamt = Request.Params["targetamt"];
        string sql = @"insert into bas_shoptargetday(shopid,shopname,targetdate,targetamt,clientcount,createdate,creater)
                        values('" + shopid + "','" + shopname + "','" + targetdate + "'," + targetamt + ","
                                  + clientcount + ",getdate(),'" + Session["UserCode"] + "')";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            Response.Write("SUCCESS");
            Response.End();
        }
        else
        {
            Response.Write("ERROR");
            Response.End();
        }
    }

    private void GetDayTargetList()
    {
        string shopid = Request.Params["shopid"];
        string pagecount = Request.Params["pagecount"];
        string sql = @"select top " + pagecount + @" shopid,shopname,convert(varchar(20),targetdate,23)as targetdate,targetamt
                        from bas_shoptargetday where shopid='" + shopid + "' order by createdate desc";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                 + "\",\"targetdate\":\"" + dr["targetdate"]
                 + "\",\"targetamt\":\"" + dr["targetamt"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void SaveTarget()
    {
        string shopid = Request.Params["shopid"];
        string shopname = Request.Params["shopname"];
        string bdate = Request.Params["bdate"];
        string edate = Request.Params["edate"];
        string targetamt = Request.Params["targetamt"];
        string sql = @"insert into bas_shoptargetweek(shopid,shopname,bdate,edate,targetamt,createdate,creater)
                        values('"+shopid+"','"+shopname+"','"+bdate+"','"+edate+"',"+targetamt+",getdate(),'"+Session["UserCode"]+"')";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            Response.Write("SUCCESS");
            Response.End();
        }
        else
        {
            Response.Write("ERROR");
            Response.End();
        }
    }

    private void GetTargetList()
    {
        string shopid = Request.Params["shopid"];
        string pagecount = Request.Params["pagecount"];
        string sql = @"select top "+pagecount+@" shopid,shopname,convert(varchar(20),bdate,23)as bdate,convert(varchar(20),edate,23)as edate,targetamt
                        from bas_shoptargetweek where shopid='" + shopid + "' order by createdate desc";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                 + "\",\"bdate\":\"" + dr["bdate"]
                 + "\",\"edate\":\"" + dr["edate"]
                 + "\",\"targetamt\":\"" + dr["targetamt"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
    private void GetTargetPageList()
    {
        string shopid = Request.Params["shopid"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
 
        string sql = @"select top "+pagecount+ @" *
                        from 
                        (
                            select row_number() over(order by createdate desc) as rownumber,
                            shopid,shopname,convert(varchar(20),bdate,23)as bdate,convert(varchar(20),edate,23)as edate,
                            targetamt from bas_shoptargetweek
                        ) A
                        where rownumber > " + showcount.ToString()+ " and shopid='" + shopid + "'";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                 + "\",\"bdate\":\"" + dr["bdate"]
                 + "\",\"edate\":\"" + dr["edate"]
                 + "\",\"targetamt\":\"" + dr["targetamt"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
}